package com.qingke.besuper.command.impl;

import com.qingke.besuper.command.UserCommand;
import com.qingke.besuper.pojo.Question;
import com.qingke.besuper.pojo.User;
import com.qingke.besuper.util.BeSuperConsole;

public class AnswerCommand extends UserCommand{

	public AnswerCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		String num=BeSuperConsole.promptNotEmpty("Please input question id:").trim();
		if(!num.replaceAll("[0-9]","").equals("")){
			BeSuperConsole.println("err input");
			return ;
		}
		int questionId=Integer.parseInt(num);
		
		Question q=dao.getQuestionByQuestionId(questionId);
		if(q==null){
			BeSuperConsole.println("do not exist the id of the question");
			return ;
		}
		if(!q.isOpen()){
			BeSuperConsole.println("the question is not opened");
			return ;
		}
		
		BeSuperConsole.println(q.getContext());
		String value=BeSuperConsole.promptNotEmpty("your answer:");
		
		int isTrue=dao.insertAnswer(value, questionId, this.user.getId());
		if(isTrue>0)
			BeSuperConsole.println("answer success");
		else
			BeSuperConsole.println("answer failed");
	}

}
